package com.example.stock.util;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.stock.App;
import com.example.stock.config.Config;
import com.example.stock.http.Api;
import com.example.stock.model.MainItemBean;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import androidx.databinding.BindingAdapter;

public class Utils {

    private static Utils mUtils;

    private static Context mContext;

    private Utils() {
    }

    public static Utils getInstance() {
        if (null == mUtils) {
            mUtils = new Utils();
        }
        return mUtils;
    }

    public static void init(Application application){
        mContext = application;
    }

    public static <T> boolean isEmpty(List<T> ts) {
        return ts == null || ts.size() == 0;
    }

    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int resId) {
        Toast.makeText(mContext, resId, Toast.LENGTH_LONG).show();
    }

    /**
     * URLEncoder编码
     */
    public static String toURLEncoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }
        try {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLEncoder.encode(str, "UTF-8");
            return str;
        } catch (Exception localException) {
        }
        return "";
    }

    /**
     * URLDecoder解码
     */
    public static String toURLDecoder(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }
        try {
            String url = new String(paramString.getBytes(), "UTF-8");
            url = URLDecoder.decode(url, "UTF-8");
            return url;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @BindingAdapter("imageSrc")
    public static void setImageSrc(ImageView imageView, MainItemBean data) {
        Glide.with(imageView.getContext())
                .load(imageView.getResources().getDrawable(data.icon)).into(imageView);
    }

    public static String getRequestXml(String method, Map<String, String> valuesMap, int SOAPVersion) {
        String xmlStr;
        if (SOAPVersion == Config.SOAP_1_2) {
            xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap12:Body>\n" +
                    "    <" + method + " xmlns=\"" + Config.URL_NAME_SPACE + "\">\n" +
                    "      " + getNodeValue(valuesMap) + "\n" +
                    "    </" + method + ">\n" +
                    "  </soap12:Body>\n" +
                    "</soap12:Envelope>";
        } else {
            xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap1=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap:Body>\n" +
                    "    <ibus:" + method + " xmlns=\"" + Config.URL_NAME_SPACE + "\">\n" +
                    "      " + getNodeValue(valuesMap) + "\n" +
                    "    </ibus:" + method + ">\n" +
                    "  </soap:Body>\n" +
                    "</soap:Envelope>";
        }
        return xmlStr;
    }

    public static String getRequestXml2(String method, Map<String, String> valuesMap, int SOAPVersion) {
        String xmlStr;
        if (SOAPVersion == Config.SOAP_1_2) {
            xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap12:Body>\n" +
                    "    <ibus:" + method + " xmlns=\"" + Config.URL_NAME_SPACE + "\">\n" +
                    "      " + getNodeValue(valuesMap) + "\n" +
                    "    </ibus:" + method + ">\n" +
                    "  </soap12:Body>\n" +
                    "</soap12:Envelope>";
        } else {
            xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap1=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap:Body>\n" +
                    "    <ibus:" + method + " xmlns=\"" + Config.URL_NAME_SPACE + "\">\n" +
                    "      " + getNodeValue(valuesMap) + "\n" +
                    "    </ibus:" + method + ">\n" +
                    "  </soap:Body>\n" +
                    "</soap:Envelope>";
        }
        return xmlStr;
    }

    private static String getNodeValue(Map<String, String> valuesMap) {
        StringBuilder str = new StringBuilder();
        for (Map.Entry<String, String> entry : valuesMap.entrySet()) {
            str.append(toStart(entry.getKey())).append(entry.getValue()).append(toEnd(entry.getKey()));
        }
        return str.toString();
    }

    private static String toStart(String key) {
        return "<" + key + ">";
    }

    private static String toEnd(String key) {
        return "</" + key + ">";
    }

    // 解析xml
    public static String analysisResult(String response, final String method) {
        String startTag = "<" + method + "Result" + ">";
        String endTag = "</" + method + "Result" + ">";
        if (response.contains(startTag) && response.contains(endTag)) {
            int startIndex = response.indexOf(startTag) + startTag.length();
            int endIndex = response.lastIndexOf(endTag);
            return response.substring(startIndex, endIndex);
        }
        return null;
    }

    public static String analysisFault(String response, final String method) {
        String startTag = "<" + method + ">";
        String endTag = "</" + method + ">";
        if (response.contains(startTag) && response.contains(endTag)) {
            int startIndex = response.indexOf(startTag) + startTag.length();
            int endIndex = response.lastIndexOf(endTag);
            return response.substring(startIndex, endIndex);
        }
        return null;
    }

}
